#include "sphere.h"

//Constructeur
Sphere::Sphere()
{
    NP = NM = 16;
    initialiser();
}

//Constructeur avec 2 paramètres
Sphere::Sphere(int NM, int NP)
    :NM(NM), NP(NP)
{
    initialiser();
}

//On initialise la sphere, on calcule la liste des sommets
void Sphere::initialiser()
{
    //On initialise les points
    for(int i = 0; i < NP; ++i)
    {
        float theta = PI / NP * i;
        for(int j = 0; j < NM; ++j)
        {
            float alpha = PI / NM * j;

            float x = cos(theta) * sin(alpha);
            float y = sin(theta);
            float z = cos(theta) * cos(alpha);

            this->sommets.append(new Point(x, y, z));
        }
    }

    //On initialise les faces
    for(int i = 0; i < NP; ++i)
    {
        for(int j = 0; j < NM; ++j)
        {
            this->faces.append(sommets[i]);
            this->faces.append(sommets[(i+1)%NP]);
            this->faces.append(sommets[NP+j]);
            this->faces.append(sommets[NP+((j+1)%NM)]);
        }
    }
}

//On dessine notre sphere
void Sphere::dessiner()
{
    glBegin(GL_QUADS);

    for(int i = 0; i < faces.count(); i+=4)
    {
        glVertex3f(faces[i]->getX(), faces[i]->getY(), faces[i]->getZ());
        glVertex3f(faces[i+1]->getX(), faces[i+1]->getY(), faces[i+1]->getZ());
        glVertex3f(faces[i+2]->getX(), faces[i+2]->getY(), faces[i+2]->getZ());
        glVertex3f(faces[i+3]->getX(), faces[i+3]->getY(), faces[i+3]->getZ());
    }

    glEnd();
}
